//
//  Fraction.m
//  OC02_HomeWork
//
//  Created by dllo on 16/7/5.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "Fraction.h"

@implementation Fraction

- (instancetype)initWithNumerator:(NSInteger)numerator denominator:(NSInteger)denominator
{
    self = [super init];
    if (self) {
        _numerator = numerator;
        _denominator = denominator;
    }
    return self;
}

+ (instancetype)fractionWithNumerator:(NSInteger)numerator denominator:(NSInteger)denominator
{
    return [[[Fraction alloc] initWithNumerator:numerator denominator:denominator] autorelease];
}

- (Fraction *)addAnotherFraction:(Fraction *)fraction
{
    NSInteger newNumerator = self.numerator * fraction.denominator + fraction.numerator * self.denominator;
    NSInteger newDenominator = self.denominator * fraction.denominator;
    Fraction *resultFraction = [Fraction fractionWithNumerator:newNumerator denominator:newDenominator];
    [resultFraction reduce];
    [resultFraction outPut];
    return resultFraction;
}

- (Fraction *)subAnotherFraction:(Fraction *)fraction
{
    NSInteger newNumerator = self.numerator * fraction.denominator - fraction.numerator * self.denominator;
    NSInteger newDenominator = self.denominator * fraction.denominator;
    Fraction *resultFraction = [Fraction fractionWithNumerator:newNumerator denominator:newDenominator];
    [resultFraction reduce];
    [resultFraction outPut];
    return resultFraction;
}

- (Fraction *)mulAnotherFraction:(Fraction *)fraction
{
    NSInteger newNumerator = self.numerator * fraction.numerator;
    NSInteger newDenominator = self.denominator * fraction.denominator;
    Fraction *resultFraction = [Fraction fractionWithNumerator:newNumerator denominator:newDenominator];
    [resultFraction reduce];
    [resultFraction outPut];
    return resultFraction;
}

- (Fraction *)divAnotherFraction:(Fraction *)fraction
{
    NSInteger newNumerator = self.numerator * fraction.denominator;
    NSInteger newDenominator = self.denominator * fraction.numerator;
    Fraction *resultFraction = [Fraction fractionWithNumerator:newNumerator denominator:newDenominator];
    [resultFraction reduce];
    [resultFraction outPut];
    return resultFraction;
}

/** 最大公约数 */
- (NSInteger)maxDivisorWithNumerator:(NSInteger)numerator
                         denominator:(NSInteger)denominator
{
    NSInteger max = 1;
    NSInteger min = numerator < denominator ? numerator : denominator;
    int minO = abs((int) min);
    for (int i = minO; i > 0; i--) {
        if (numerator % i == 0 && denominator % i == 0) {
            max = i;
            break;
        }
    }
    return max;
}

/** 约分 */
- (void)reduce
{
    NSInteger max = [self maxDivisorWithNumerator:self.numerator denominator:self.denominator];
    self.numerator /= max;
    self.denominator /= max;
}

- (void)outPut
{
    if (self.denominator == 0) {
        NSLog(@"不存在该分数");
    } else if (self.denominator == self.numerator) {
        NSLog(@"结果为1");
    } else if (self.denominator + self.numerator == 0) {
        NSLog(@"结果为-1");
    } else if (self.numerator == 0) {
        NSLog(@"结果为0");
    } else {
        NSLog(@"结果为%ld/%ld", self.numerator, self.denominator);
    }
}

- (NSString *)description
{
    return [NSString stringWithFormat:@"%ld/%ld", self.numerator, self.denominator];
}
@end
